/*
*    swsyd - swsy daemon, make nsswitch easy
*    Copyright (C) 2024  Marcus Pedersén marcus@marcux.org
*
*    This program is free software: you can redistribute it and/or modify
*    it under the terms of the GNU General Public License as published by
*    the Free Software Foundation, either version 3 of the License, or
*    (at your option) any later version.
*
*    This program is distributed in the hope that it will be useful,
*    but WITHOUT ANY WARRANTY; without even the implied warranty of
*    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
*    GNU General Public License for more details.
*
*    You should have received a copy of the GNU General Public License
*    along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

package common

import (
	"os"
	"fmt"
	"log/slog"
	"strings"
	"path/filepath"
	"github.com/spf13/viper"
)

// Default config
const CONFIGFILE string = "swsyd.conf"
const CONFIGPATH string = "/etc/swsy/"
const PORT uint16 = 9779
const DBFILE string = "/var/lib/swsy/swsyd.db"
const LOGFILE string = "/var/log/swsy/swsyd.log"
const MinUID = 10000
const MinGID = 10000
const DefaultHome = "/home"
const DefaultShell = "/usr/bin/bash"



type Config struct {
	Port uint16
	Dbfile string
	Logfile string
	Loglevel slog.Level
	MinUid uint64
	MinGid uint64
	DefaultHome string
	DefaultShell string
}


// Parse config file
// if values are missing
// default is used
func GetConfig() Config {
	conf := Config{}
	viper.SetConfigName(CONFIGFILE)
	viper.SetConfigType("toml")
	viper.AddConfigPath(CONFIGPATH)
	
	if err := viper.ReadInConfig(); err != nil {
		fmt.Fprintf(os.Stderr, "Failed to read config file: %s\n%s\n",
			filepath.Join(CONFIGPATH, CONFIGFILE), err)
		fmt.Println("Using default config values")

		conf.Port = PORT
		conf.Dbfile = DBFILE
		conf.Logfile = LOGFILE
		conf.Loglevel = slog.LevelInfo
		conf.MinUid = MinUID
		conf.MinGid = MinGID
		conf.DefaultHome = DefaultHome
		conf.DefaultShell = DefaultShell
	} else {
		conf.Port = viper.GetUint16("swsyd.port")
		if conf.Port == 0 {
			conf.Port = PORT
		}

		conf.MinUid = viper.GetUint64("swsyd.minuid")
		if conf.MinUid == 0 {
			conf.MinUid = MinUID
		}

		conf.MinGid = viper.GetUint64("swsyd.mingid")
		if conf.MinGid == 0 {
			conf.MinGid = MinGID
		}

		conf.DefaultHome = viper.GetString("swsyd.defaulthome")

		if len(conf.DefaultHome) == 0 {
			conf.DefaultHome = DefaultHome
		}

		conf.DefaultShell = viper.GetString("swsyd.defaultshell")

		if len(conf.DefaultShell) == 0 {
			conf.DefaultShell = DefaultShell
		}
		
		conf.Dbfile = viper.GetString("swsyd.dbfile")

		if len(conf.Dbfile) == 0 {
			conf.Dbfile = DBFILE
		}

		conf.Logfile = viper.GetString("swsyd.logfile")

		if len(conf.Logfile) == 0 {
			conf.Logfile = LOGFILE
		}

		level := strings.ToLower(viper.GetString("swsyd.loglevel"))

		if level == "debug" {
			conf.Loglevel = slog.LevelDebug
		} else if level == "warn" {
			conf.Loglevel = slog.LevelWarn
		} else if level == "error" {
			conf.Loglevel = slog.LevelError
		} else {
			conf.Loglevel = slog.LevelInfo
		}
	}
	
	return conf
}
